
const express = require('express');
const router = express.Router();
const Post = require('../models/Post');
const auth = require('../middleware/auth');

// 获取当前用户的帖子
router.get('/my-posts', auth, async (req, res) => {
  try {
    const posts = await Post.find({ author: req.user.id });
    res.json(posts);
  } catch (error) {
    res.status(500).json({ message: '获取帖子失败' });
  }
});

// 删除帖子
router.delete('/:id', auth, async (req, res) => {
  try {
    const post = await Post.findOneAndDelete({
      _id: req.params.id,
      author: req.user.id
    });

    if (!post) {
      return res.status(404).json({ message: '帖子未找到或无权删除' });
    }

    res.json({ message: '帖子删除成功' });
  } catch (error) {
    res.status(500).json({ message: '删除帖子失败' });
  }
});

module.exports = router;